package tag.in;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import tag.in.data.Tag;

/**
 * Custom adapter for "Tag" model objects.
 * 
 * @author andre
 */
public class TagAdapter extends BaseAdapter {

    private static final String CLASSTAG = TagAdapter.class.getSimpleName();
    private final Context context;
    private final List<Tag> tags;
    private final String[] categories;
    
    public TagAdapter(Context context, List<Tag> tags, String[] categories) {
        this.context = context;
        this.tags = tags;
        this.categories = categories;
        
        Log.v(Constants.LOGTAG, " " + TagAdapter.CLASSTAG + " reviews size - " + this.tags.size());
    }

    public int getCount() {
        return this.tags.size();
    }

    public Object getItem(int position) {
        return this.tags.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Tag tag = this.tags.get(position);
        
        return new TagListView(this.context, position, tag.name, "Categoria: " + this.categories[tag.category] + ", Criado Por: "+tag.owner);
    }

    /**
     * TagListView that adapter returns as it's view item per row.
     * 
     * @author andre
     */
    private final class TagListView extends LinearLayout {

        private TextView name;
        private TextView info;
        
        public TagListView(Context context, final int index, String name, String info) {

            super(context);
            setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 3, 5, 0);
            
            /*
            if(index%2 == 0) {
            	this.setBackgroundColor(Color.DKGRAY);
            }
            */
            
            this.name = new TextView(context);
            this.name.setText(name);
            this.name.setTextSize(16f);
            this.name.setTextColor(Color.WHITE);
            this.addView(this.name, params);
            
            this.info = new TextView(context);
            this.info.setText(info);
            this.info.setTextSize(14f);
            this.info.setTextColor(Color.GRAY);
            this.addView(this.info, params);
        }
    }
}
